#include "Requests.h"
#include "WindowManager.h"
#include <QDateTime>
#include <QProcess>
#include <QFile>
#include <QDir>
#include "Satory.h"
#include "Dream.h"
BaseRequest::BaseRequest(Satory* master):Master(master),root(Json::objectValue)
{

}
void RegisterRequest::OnError(Json::Value& error)
{
    NotifWindow* window=(NotifWindow*)WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW);
    window->SetText("Such name is already taken");
    Sync::RemoveState(TRegisterRequest);
};
void RegisterRequest::SetKey(const std::string& key)
{
     mPrivateKey=key;
}

void RegisterRequest::OnAnswer(Json::Value& answer)
{

    QString userId=answer["result"]["userId"].asString().c_str();
    Satory::GetInstance()->SetName(answer["result"]["userId"].asString());
    QString message("User ");
    message+=userId+" registered succesfully";
    WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW,&message);
    Satory::GetInstance()->SaveKey(mPrivateKey);


    Sync::RemoveState(TRegisterRequest);
    WindowManager::GetInstance()->DoCommand(WindowManager::LOGINWINDOW,"set_def_id",&userId);
    WindowManager::GetInstance()->CloseWindow(WindowManager::REGISTERWINDOW);
     Satory::GetInstance()->ResetRSA();
     Satory::GetInstance()->SetName("");
};

void LoginRequest::OnError(Json::Value& error)
{
    Sync::RemoveState(TLoginRequest);
    QString msg(error["error"]["message"].asString().c_str());
    WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW,(void*)(&msg));
    Satory::GetInstance()->ResetRSA();
    Satory::GetInstance()->SetName("");
}

void LoginRequest::OnAnswer(Json::Value& answer)
{
    Sync::RemoveState(TLoginRequest);
    Satory::GetInstance()->InitRoster(QString(root["params"]["userId"].asString().c_str()));

}
void VerifyRequest::OnError(Json::Value& error)
{
    Sync::RemoveState(TVerifyRequest);
}

void VerifyRequest::OnAnswer(Json::Value& answer)
{
    Sync::RemoveState(TVerifyRequest);
}

void PrivateKeyGetRequest::OnError(Json::Value& error)
{
    if(error["error"]["code"]=="103")
    {
        NotifWindow* win=(NotifWindow*)WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW);
        win->SetText("Incorrect email code");
    }
    if(error["error"]["code"]=="101")
    {
        NotifWindow* win=(NotifWindow*)WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW);
        win->SetText("User is not registered");
    }
    Sync::RemoveState(TPrivateKeyGetRequest);

}

void PrivateKeyGetRequest::OnAnswer(Json::Value& answer)
{
    std::string key=answer["result"]["privateKeyEncrypted"].asString();
    Satory::GetInstance()->SaveKey(key);
    WindowManager::GetInstance()->CloseWindow(WindowManager::KEYGETWINDOW);
    NotifWindow* win=(NotifWindow*)WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW);
    win->SetText("Private key successfully restored from server");

    Sync::RemoveState(TPrivateKeyGetRequest);
}
void PublicKeyGetRequest::OnError(Json::Value& error)
{
    Sync::RemoveState(TPublicKeyGetRequest);
    QString msg(error["error"]["message"].asString().c_str());
    WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW,(void*)(&msg));
};
void PublicKeyGetRequest::OnAnswer(Json::Value& answer)
{
    Sync::RemoveState(TPublicKeyGetRequest);

    emit KeyArrived(this,answer);
};
void ResetKeyRequest::SetKey(const std::string& key)
{
    mPrivateKey=key;
}
void ResetKeyRequest::OnError(Json::Value& error)
{
    if(error["error"]["code"]=="103")
    {
        NotifWindow* win=(NotifWindow*)WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW);
        win->SetText("Incorrect email code");
    }
    if(error["error"]["code"]=="101")
    {
        NotifWindow* win=(NotifWindow*)WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW);
        win->SetText("User is not registered");
    }
    Sync::RemoveState(TResetKeyRequest);
}

void ResetKeyRequest::OnAnswer(Json::Value& answer)
{
    Satory::GetInstance()->ResetRSA();
    Satory::GetInstance()->SaveKey(mPrivateKey);


    WindowManager::GetInstance()->CloseWindow(WindowManager::KEYGETWINDOW);
    NotifWindow* win=(NotifWindow*)WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW);
    win->SetText("Private key successfully recreated");

    Sync::RemoveState(TResetKeyRequest);

}
void AskAuthRequest::OnError(Json::Value& error)
{
    Sync::RemoveState(TAskAuthRequest);
};
void AskAuthRequest::OnAnswer(Json::Value& answer)
{
    PeopleWindow* win=dynamic_cast<PeopleWindow*>(WindowManager::GetInstance()->GetWindow(WindowManager::PEOPLEWINDOW));
    if(win)
    {
        string user=root["params"]["userId"].asString();
        QListWidgetItem* item=win->AddPerson(user.c_str());
        Dream* dream=Satory::GetInstance()->GetDream(user.c_str());
        if(dream)
            dream->AttachUi(Dream::PERSON,item);
    }
    Sync::RemoveState(TAskAuthRequest);
};
void SendMessageRequest::OnError(Json::Value& error)
{
    Sync::RemoveState(TSendMessageRequest);
}

void SendMessageRequest::OnAnswer(Json::Value& answer)
{
    Sync::RemoveState(TSendMessageRequest);
}

void ConfirmAuthRequest::OnError(Json::Value& error)
{
    Sync::RemoveState(TConfirmAuthRequest);
};
void ConfirmAuthRequest::OnAnswer(Json::Value& answer)
{
    Sync::RemoveState(TConfirmAuthRequest);
};
void ChatKeyGetRequest::OnError(Json::Value& error)
{
    Sync::RemoveState(TChatKeyGetRequest);
}
void ChatKeyGetRequest::OnAnswer(Json::Value& answer)
{
    Sync::RemoveState(TChatKeyGetRequest);
    emit KeyArrived(this,answer);
}
